local skel = fk.CreateSkill {
  name = "emo__zhuhua",
}

Fk:loadTranslationTable{
  ["emo__zhuhua"] = "逐华",
  [":emo__zhuhua"] = "出牌阶段限一次，你可以重铸所有手牌，当你使用或打出最后一张重铸获得的牌时，你摸牌至X张（X为上次重铸牌数，至多为5）。",

  ["#emo__zhuhua"] = "飞花：你可以重铸所有手牌",
  ["@emo__zhuhua"] = "飞花",
  ["@@emo__zhuhua-inhand"] = "飞花",
}

skel:addEffect("active", {
  anim_type = "drawcard",
  prompt = "#emo__zhuhua",
  target_num = 0,
  card_num = 0,
  times = function (self, player)
    return 1 - player:usedEffectTimes(self.name, Player.HistoryPhase)
  end,
  can_use = function (self, player)
    return player:usedEffectTimes(self.name, Player.HistoryPhase) == 0 and not player:isKongcheng()
  end,
  on_use = function (self, room, use)
    local player = use.from
    room:setPlayerMark(player, "@emo__zhuhua", math.min(5, player:getHandcardNum()))
    room:recastCard(player:getCardIds("h"), player, skel.name, "@@emo__zhuhua-inhand")
  end,
})

local can_trigger = function (self, event, target, player, data)
    if target == player and not player.dead then
      return data.extra_data and data.extra_data.emo__zhuhua_draw
    end
  end

local on_use = function (self, event, target, player, data)
    local n = data.extra_data.emo__zhuhua_draw - player:getHandcardNum()
    if n > 0 then
      player:drawCards(n, skel.name)
    end
  end

skel:addEffect(fk.CardUsing, {
  is_delay_effect = true,
  can_trigger = can_trigger,
  on_cost = Util.TrueFunc,
  on_use = on_use,
})

skel:addEffect(fk.CardResponding, {
  is_delay_effect = true,
  can_trigger = can_trigger,
  on_cost = Util.TrueFunc,
  on_use = on_use,
})

local can_refresh = function (self, event, target, player, data)
    if target == player and player:getMark("@emo__zhuhua") > 0 then
      local subcards = Card:getIdList(data.card)
      local mark = table.filter(player:getCardIds("h"), function (id)
        return Fk:getCardById(id):getMark("@@emo__zhuhua-inhand") ~= 0
      end)
      return #mark > 0 and table.every(mark, function (id) return table.contains(subcards, id) end)
    end
  end

local on_refresh = function (self, event, target, player, data)
    data.extra_data = data.extra_data or {}
    data.extra_data.emo__zhuhua_draw = player:getMark("@emo__zhuhua")
    player.room:setPlayerMark(player, "@emo__zhuhua", 0)
  end

skel:addEffect(fk.PreCardUse, {
  can_refresh = can_refresh,
  on_refresh = on_refresh
})

skel:addEffect(fk.PreCardRespond, {
  can_refresh = can_refresh,
  on_refresh = on_refresh
})

return skel
